const db = require('../../models/index');
const DAO = require('../../dao/sql-option-dao');
const logger = require('../../utils/logger-util');
const Tags = db.tags;
const Article = db.article;

// 创建标签
exports.create = async (req, res) => {
    const createInfo = req.body;
    let findTag = await Tags.findOne({ where: {'tag_name': createInfo.tagName} });
    if(findTag && findTag.id) return res.sendResultAto(null, 605, '标签已存在！');
    DAO.create(Tags, createInfo, data => {
        res.sendResult(data);
    });
};

// 查询全部标签
exports.findAll = (req, res) => {
    const fAllInfo = req.body;
    // fAllInfo.include = [
    //     { model: Article },
    // ];
    DAO.list(Tags, fAllInfo, list => {
        res.sendResult(list);
    });
};

// 查询单个
exports.findOne = (req, res) => {
    const fOneInfo = req.body;
    fOneInfo.include = [
        { model: Article },
    ];
    DAO.findOne(Tags, fOneInfo, data => {
        res.sendResult(data);
    });
};

// 更新
exports.update = (req, res)=> {
    const updateInfo = req.body;
    DAO.update(Tags, updateInfo, {id:updateInfo.id}, data => {
        res.sendResult(data);
    });
};

// 删除
exports.delete = (req, res) => {
    const deleteInfo = req.body;
    DAO.delete(Tags, { id:deleteInfo.id }, data => {
        res.sendResult(data);
    });
};

// 删除全部
exports.deleteAll = (req, res) => {
    const dAllInfo = req.body;
    DAO.deleteAll(Tags, data => {
        res.sendResult(data);
    });
};